package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 用户添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 截取用户 ID
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.listShoppingCart(shoppingCart);
        // 如果存在则更新数量
        if(list != null && list.size() > 0) {
            // 因为每个商品在购物车中只有一条数据，所以直接获取
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            // 更新数量
            shoppingCartMapper.updateNumber(cart);
        } else {
            // 如果不存在则插入数据
            // 查询添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            // 如果是菜品
            if(dishId != null) {
                DishVO dishVO = dishMapper.getById(dishId);
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setName(dishVO.getName());
            } else {
                // 如果是套餐
                SetmealVO setmealVO = setMealMapper.selectSetMealDataById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setName(setmealVO.getName());
            }
            // 相同字段统一更新
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 插入数据
            shoppingCartMapper.addData(shoppingCart);
        }

    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> checkShoppingCart() {
        // 获取用户 ID
        Long userId = BaseContext.getCurrentId();
        // 构建用户
        ShoppingCart user = ShoppingCart.builder().id(userId).build();
        List<ShoppingCart> list = shoppingCartMapper.listShoppingCart(user);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteShoppingCart(userId);
    }
}
